<template>
  <div>
    <div v-if="state == 1">

      <div v-if="!isH5">
        <index-page></index-page>
      </div>
      <div v-else>
        <loginH5></loginH5>
      </div>
    </div>
    <div v-if="state == 4"
      style="display: flex;justify-content: center;align-items: center;font-size: 20px;font-weight: bold;margin-top: 200px;">
      请联系经纪人获取正确的投保链接
    </div>
  </div>
</template>

<script>
import indexPage from './pages/index';
import loginH5 from '@/pages/login/login_h5.vue'
export default {
  components: { indexPage, loginH5 },
  data() {
    return {
      isH5: false,
      isMobile: '',
      state: 1,
      allowPrefix: ['wx', '192'],
      no:''
    }


  },
  mounted() {
    // this.isMobile = /Android|webOS|iPhone|iPod|BlackBerry|iPad|Windows Phone/i.test(navigator.userAgent);
    // this.ajaxs("index/check", {
    //   success: (res) => {

    //     if (res.code != 1 && this.isMobile) {
    //       var viewport = document.querySelector('meta[name="viewport"]');
    //       viewport.setAttribute('content', 'width=device-width,' +
    //         'initial-scale=1.0' +
    //         'user-scalable=yes,shrink-to-fit=no');
    //       this.isH5 = true

    //     }
    //   },
    // });

  },
  methods: {
    checkDomain() {
      {
        let host = window.location.host;
 
        this.no = this.$route.query.no;
        // 取主机名的第一个部分作为前缀
        let prefix = host.split('.')[0];
        if (!this.allowPrefix.includes(prefix) || this.no) {

          this.state = 4;
          // 这里可以自定义提示方式
          this.toast(0, "请联系经纪人获取正确的投保链接");
          this.$router.push('/index/login/login')
          // 可选：跳转到指定页面或直接关闭页面
          // window.location.href = 'https://md.saigaocloud.com/';
        } else {
          this.state = 1
          console.log('state=1');

          // var viewport = document.querySelector('meta[name="viewport"]');
          // // 判断是否为手机环境
          // this.isMobile = /Android|webOS|iPhone|iPod|BlackBerry|iPad|Windows Phone/i.test(navigator.userAgent);
          // if (this.isMobile) {
          //   this.isH5 = true
          //   // viewport.setAttribute('content', 'width=device-width,' +
          //   //   'initial-scale=1.0' +
          //   //   'user-scalable=yes,shrink-to-fit=no');
          //   // this.$router.push('/index/login/login_h5')
          // } else {
          //   viewport.setAttribute('content', 'width=device-width,' +
          //     'initial-scale=0.1,' +
          //     'user-scalable=yes,shrink-to-fit=no');
          //   this.isH5 = false
          // }
        }
      }
    },
  },
  watch: {
    '$route.path'(newVal) {
      console.log('监听路由');
      console.log(newVal);

      this.ajaxs("index/getSellerNoList", {
        success: (res) => {
          this.allowPrefix = this.allowPrefix.concat(res.msg);
          if (this.allowPrefix.length > 0) {
            this.checkDomain()
          }

        },
      });
      var viewport = document.querySelector('meta[name="viewport"]');
      // 只要路由不是 index/login/login_h5 就设置 isH5 为 true
      if (newVal !== '/index/login/login_h5') {
        viewport.setAttribute('content', 'width=device-width,' +
          'initial-scale=0.32' +
          'user-scalable=yes,shrink-to-fit=no');
        this.isH5 = false;

      } else {

        viewport.setAttribute('content', 'width=device-width,' +
          'initial-scale=1.0' +
          'user-scalable=yes,shrink-to-fit=no');
        this.isH5 = true
      }
    }
  }
}
</script>

<style>
@import "assets/css/app.css";
</style>
